package com.itheima.web.controller;


import com.github.pagehelper.PageInfo;
import com.itheima.domain.Category;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/category")
public class CategoryController {
    /**
     * 分页查询
     */

    @Autowired
    CategoryService categoryService;

    @RequestMapping("/findByPage")
    @ResponseBody
    public  PageInfo<Category> findByPage(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize" ,defaultValue = "5") Integer pageSize ){
        PageInfo<Category> pageInfo = categoryService.findByPage(pageNum, pageSize);
        return pageInfo;
    }


    @RequestMapping("/save")
    public  String  saveCategory(Category category){
        System.out.println(category);
        if(category.getId() == null){
             categoryService.saveCategory(category);
            
        }else {
            
        System.out.println(category);
        categoryService.upDate(category);
        }
             return "redirect:/category_list.html";

    }

    /**
     * 未查出
     * @param id
     * @return
     */
    @RequestMapping("/findCategoryById")
    @ResponseBody
    public Category findCategoryById(Integer id){
        System.out.println(id);
        return categoryService.findCategoryById(id);
    }

    /**
     * 单个删除
     * @param id
     * @return
     */
    @RequestMapping("/deleteById")
    @ResponseBody
    public String deleteById(Integer id){

        categoryService.deleteById(id);
        return "ok";
    }


    @RequestMapping("/deleteSelect")
    @ResponseBody
    public String deleteSelect(Integer[] ids){

        categoryService.deleteSelect(ids);
        return "ok";
    }
}
